﻿using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Text;


namespace Separation.Token
{
    public class MemoryCacheHelper
    {
        public static MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());

        public static bool Exists(string key)
        {
            if (key == string.Empty) 
            {
                throw new NullReferenceException(nameof(key));
            }
            object cached;
            return Cache.TryGetValue(key, out cached);
        }

        public static object GetCache(string key) 
        {
            if (key == string.Empty)
            {
                throw new NullReferenceException(nameof(key));
            }
            return Cache.Get(key);
        }

        public static bool AddMemoryCacheAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiresAbsoulte) 
        {
            if (key == string.Empty)
            {
                throw new NullReferenceException(nameof(key));
            }
            if (value == null)
            {
                throw new NullReferenceException(nameof(key));
            }

            Cache.Set(
                key, value, 
                new MemoryCacheEntryOptions()
                .SetSlidingExpiration(expiresSliding)
                .SetAbsoluteExpiration(expiresAbsoulte));

            return Exists(key);
        }

    }
}
